package com.yejiali.backend.controller;
import com.yejiali.backend.entity.dto.PatientsDTO;
import com.yejiali.backend.response.Response;
import com.yejiali.backend.service.AuditService;
import com.yejiali.backend.service.PatientsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * (Patients)表控制层
 *
 * @author yejiali
 * @since 2025-05-15 11:14:23
 */
@RestController
@RequestMapping("patients")
@Tag(name = "病人信息管理", description = "病人信息接口")
public class PatientsController{
   @Autowired
    private PatientsService patientsService;



    /**
     * 按创建时间查询分页最近记录（用于初始化表格）
     * @return
     */
    @GetMapping("/initData")
    @Operation(summary = "获取初始化病人数据", description = "根据录入时间分页显示病人信息")
    public Response initData(@RequestParam("currentPage") int currentPage, @RequestParam("pageSize") int pageSize){
        return patientsService.initData(currentPage,pageSize);
    }


    /**
     * 由病人id查询
     * @param patientId
     * @return
     */
    @GetMapping("/queryById")
    @Operation(summary = "获取病人详情(patientId)", description = "根据patientId获取病人信息")
    public Response queryById(@RequestParam("patientId") Long patientId){
        return patientsService.queryById(patientId);
    }
    /**
     * 由姓名查询
     * @param patientName
     * @return
     */
    @GetMapping("/queryByName")
    @Operation(summary = "获取病人详情(patientName)", description = "根据patientName获取病人信息")
    public Response queryByName(@RequestParam("patientName") String patientName,
                                @RequestParam("currentPage") int currentPage,
                                @RequestParam("pageSize") int pageSize){
        return patientsService.queryByName(patientName,currentPage,pageSize);

    }
    /**
     * 由身份证查询
     * @param idCard
     * @return
     */
    @GetMapping("/queryByIdCard")
    @Operation(summary = "获取病人详情(idCard)", description = "根据idCard获取病人信息")
    public Response queryByIdcard(@RequestParam("idCard") String idCard){

        return patientsService.queryByIdcard(idCard);
    }


    /**
     * 由病人id删除
     * @param patientId
     * @return
     */
    @PreAuthorize("hasRole('SUPER_AUDITOR')")
    @DeleteMapping("/deleteById")
    @Operation(summary = "删除病人信息(patientId)", description = "根据patientId删除病人信息")
    public Response deleteById(@RequestParam("patientId") Long patientId){
        return patientsService.deleteById(patientId);
    }

    /**
     * 由身份证号删除
     *
     */

    @PreAuthorize("hasRole('SUPER_AUDITOR')")
    @DeleteMapping("/deleteByIdCard")
    @Operation(summary = "删除病人信息(idCard)", description = "根据idCard删除病人信息")
    public Response deleteByIdCard(@RequestParam("idCard") String idCard){
        return patientsService.deleteByIdCard(idCard);
    }
    /**
     * 新增病人档案
     * @param
     * @return
     */
    @PostMapping("/add")
    @Operation(summary = "新建病人档案", description = "病人详细信息录入系统")
    public Response add(@Validated @RequestBody PatientsDTO patientsDTO){
        return patientsService.add(patientsDTO);
    }


    /**
     * 通过查询病人病例
     * @param patientId
     * @return
     */
    @GetMapping("/getCasesById")
    @Operation(summary = "查询病人病例", description = "根据patientId查询病人历史病例")
    public Response getCasesByPatientId(@RequestParam("patientId") Long patientId){
        return patientsService.getCasesByPatientId(patientId);
    }
}

